﻿using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgramMain.Database;
using ProgramMain.Framework;

namespace ProgramMain.OpticNet
{
    
    public partial class FibrePointList : ProgramMain.Framework.BaseGridListCtl
    {
        public FibrePointList()
        {
            InitializeComponent();
        }

        private int _vertexID;
        public int VertexID
        {
            get { return _vertexID; }
            set { _vertexID = value; }
        }

        private int?  _fibreINID = null;
        public int? FibreINID
        {
            get { return _fibreINID; }
            set { _fibreINID = value; }
        }

        private int? _fibreOutID = null;
        public int? FibreOutID
        {
            get { return _fibreOutID; }
            set { _fibreOutID = value; }
        }

        public bool AutoClose { get; set; }
        public event EventHandler AfterDeleteFibrePoint = null;
       
        protected override void LoadData()
        {
            //прочитать данные из базы и заполнить датасет
            this.fibrePointTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            fibrePointTableAdapter.Fill(simpleOpticNetDB.FibrePoint, DetailID, VertexID, null, FibreINID, FibreOutID);
            UpdateStatusText(this.simpleOpticNetDB.FibrePoint);
        }

        protected override void UpdateData()
        {
            //внести изменения в базу
            this.fibrePointTableAdapter.SetUpdateTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.fibrePointTableAdapter.Update(this.simpleOpticNetDB.FibrePoint);
            UpdateStatusText(this.simpleOpticNetDB.FibrePoint);
        }
        
        override protected BaseModalDetailCtl CreateDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode)
        {
            FibrePointDetail fibrePointDetail = new FibrePointDetail();
            if (pShowMode == BaseModalDetailCtl.ShowModeTypes.Delete) fibrePointDetail.SaveCaption = "Разорвать";
            fibrePointDetail.AutoClose = AutoClose;
            return fibrePointDetail;
        }

        override protected void AfterChangeDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode, DataRowView RowView)
        {
            base.AfterChangeDetailDialog(pShowMode, RowView);

            if (pShowMode == BaseModalDetailCtl.ShowModeTypes.Delete)
            {
                if (AfterDeleteFibrePoint != null)
                    AfterDeleteFibrePoint.Invoke(this, EventArgs.Empty);
            }
        }

        public bool AddFibrePoint(int fibreIn, int fibreOut, int vertexID)
        {
            try
            {
                this.dataSource.AddNew();
                SimpleOpticNetDB.FibrePointRow fibrePointRow =
                    (SimpleOpticNetDB.FibrePointRow)((DataRowView)this.dataSource.Current).Row;
                fibrePointRow.VertexID = vertexID;
                fibrePointRow.FINID = fibreIn;
                fibrePointRow.FOUTID = fibreOut;
                fibrePointRow.Point = 0;
                fibrePointRow.UserID = ProgramMain.Framework.BaseSecurity.UserID;
                this.dataSource.EndEdit();
                fibrePointTableAdapter.Update(fibrePointRow);
                DBRead();
            }
            catch(Exception exception)
            {
                XtraMessageBox.Show(exception.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private void FibrePointList_DetailIDChanged(object sender, DetailEventArgs e)
        {
            DBRead();
        }

        protected override void Action_Edit(object sender, EventArgs e)
        {
        }

        public void DeleteConnection(int PlanType)
        {
            DBRead();
            if (dataSource.Count != 1) return;
            ((DataRowView) dataSource.Current)["PlanType"] = PlanType;
            biDelete.PerformClick();
        }
    }
}
